﻿using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace ui
{
    internal class ParseUtil
    {
        //声明一个更新主线程的委托
        public delegate void UpdateUi(ParseResult rs);
        //定义一个委托类型变量
        public static UpdateUi UpdateDashBoard;
        // 解析: 寻找 @# 与 \r\n\0 找到后开始具体解析
        private const byte Header1 = (byte)'@';
        private const byte Header2 = (byte)'#';
        private const byte End1 = (byte)'\r';
        private const byte End2 = (byte)'\n';
        private const byte End3 = (byte)'\0';
        private const char Sep = ',';


        private const int Len = 1024;
        private static int _store;
        private static bool _startFlag;
        private static readonly byte[] Buffer = new byte[Len];
        private static readonly byte[] Temp = new byte[2];
        private static string str_data;


        public static void Parse_updata(byte[] arg, int len)
        {
            var res = new ParseResult();
            str_data = System.Text.Encoding.Default.GetString(arg);
            //Console.WriteLine("开始解析数据");
            //Console.WriteLine("接收到的原始数据");
            //Console.WriteLine(str_data);
            foreach (Match mx in Regex.Matches(str_data, @"@#,([^\r]*)\r\n"))
            {
                string temp = mx.Value;
                //Console.WriteLine(temp);
                string[] arr = mx.Value.Split(',');

                for (int i = 0; i < arr.Length; i++)
                {
                    arr[i] = arr[i].Trim();
                    Console.Write(arr[i] + " ");
                }
                //Console.WriteLine("");
                if (arr[0] == "@#")
                {
                    res.Torque1 = double.Parse(arr[1]);
                    res.Torque2 = double.Parse(arr[2]);
                    res.Torque3 = double.Parse(arr[3]);
                    res.Torque4 = double.Parse(arr[4]);
                    res.Speed1 = double.Parse(arr[5]) / 10.0;
                    res.Speed2 = double.Parse(arr[6]) / 10.0;
                    res.Speed3 = double.Parse(arr[7]) / 10.0;
                    res.Speed4 = double.Parse(arr[8]) / 10.0;
                    res.Press1 = double.Parse(arr[9]);      //IN4   压力2
                    res.Press2 = double.Parse(arr[10]);      //IN5   压力3
                    res.Press3 = double.Parse(arr[11]);      //IN6   压力4
                    res.Press4 = double.Parse(arr[12]);      //IN7   温度1
                    res.Temp1 = double.Parse(arr[13]);      //IN8  温度2
                    res.Temp2 = double.Parse(arr[14]);      //IN9   温度3
                    res.Temp3 = double.Parse(arr[15]);      //IN10   
                    res.FlowRt = double.Parse(arr[16]);      //IN1
                    res.Adc8 = double.Parse(arr[17]);        //IN2
                    res.Height = double.Parse(arr[18]);      //IN3   压力1
                    res.Status1 = double.Parse(arr[19]);
                    res.Status2 = double.Parse(arr[20]);
                    res.Status3 = double.Parse(arr[21]);
                    res.Status4 = double.Parse(arr[22]);
                    //Console.WriteLine("转换成功" + res.ToString());
                    
                    // WriteData(res);
                    UpdateDashBoard(res);      //解析函数解析完毕后通过委托交给显示函数
                }
            }
            
        }


            
    }
}
